﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace BtoBSWT315.User
{
    public partial class Add_sj : System.Web.UI.Page
    {

        public int SortID, TypeID;
        private int Gsid, Hybz;

        private void Page_Load(object sender, System.EventArgs e)
        {
            // 在此处放置用户代码以初始化页面
            if (Session["user"] == null)
            {
                Response.Redirect("../Default.aspx");
            }
            if (!IsPostBack)
            {
                EditDataBind();
            }
        }

        protected string GetImageUrl(string imgUrl)
        {
            imgUrl = imgUrl.Replace("..", "~/");
            imgUrl = imgUrl.Replace("\\", "/");
            imgUrl = imgUrl.Trim();
            return imgUrl;
        }

        private void EditDataBind()
        {
            // string user = Session["user"].ToString();
            Admin.Admin_ClassDb classdb = new Admin.Admin_ClassDb();
            string sql = "select * from qyml where [user]='" + Session["user"] + "'";
            DataTable dataTable = null;
            dataTable = classdb.GetTables(sql);
            if (dataTable != null)
            {
                foreach (DataRow row in dataTable.Rows)
                {
                    linkman.Text = row["name"].ToString();
                    phone.Text = row["phone"].ToString();
                    company.Text = row["qymc"].ToString();
                    address.Text = row["address"].ToString();
                    country.Text = row["sf"].ToString();
                    city.Text = row["city"].ToString();
                    email.Text = row["email"].ToString();
                    fax.Text = row["fax"].ToString();
                    web.Text = row["web"].ToString();
                    mobile.Text = row["mobile"].ToString();
                }
                dataTable = null;
            }


            //Admin.Admin_ClassDb classdb = new Admin.Admin_ClassDb();

            //定义所需SQL语句
            string sql2 = "select * from class_1";
            //引入数据库操作类
            //DataTable dataTable = null;
            dataTable = classdb.GetTables(sql2);
            if (dataTable != null)
            {
                BigClass.DataSource = dataTable.DefaultView;
                BigClass.DataTextField = "Sort";
                BigClass.DataValueField = "SortID";
                BigClass.DataBind();
                //BigClass.SelectedValue = sortId;        //行业大类
                BigClass.SelectedIndex = 0;
                dataTable = null;
            }

            //定义所需SQL语句
            string sql3 = "select * from class_2 where sortid=" + BigClass.SelectedValue;

            dataTable = classdb.GetTables(sql3);
            if (dataTable != null)
            {
                SmallClass.DataSource = dataTable.DefaultView;
                SmallClass.DataTextField = "TypeName";
                SmallClass.DataValueField = "TypeID";
                SmallClass.DataBind();
                //SmallClass.SelectedValue = typeId;           //行业小类
                SmallClass.SelectedIndex = 0;
            }
        }


        private void BigClass_SelectedIndexChanged(object sender, System.EventArgs e)
        {
            //定义所需SQL语句
            string sql4 = "select * from class_2 where sortid=" + BigClass.SelectedValue;
            //引入数据库操作类
            Admin.Admin_ClassDb classdb = new Admin.Admin_ClassDb();
            DataTable dataTable = null;

            dataTable = classdb.GetTables(sql4);
            if (dataTable != null)
            {
                SmallClass.DataSource = dataTable.DefaultView;
                SmallClass.DataTextField = "TypeName";
                SmallClass.DataValueField = "TypeID";
                SmallClass.DataBind();                //行业小类
            }
        }


        private void Button1_Click(object sender, System.EventArgs e)
        {
            if (string.IsNullOrEmpty(showname.Text) || string.IsNullOrEmpty(content.Text))
            {
                Response.Write("<script>alert('请按要求输入');</script>");
                Response.Write("<script>history.go(-1);</script>");
                Response.End();
            }

            //引入数据库操作类
            Admin.Admin_ClassDb classdb = new Admin.Admin_ClassDb();
  
            bool IsVIP = classdb.CheckVIP(Session["user"].ToString());

            string checkInfoCount = "select count(*) as [info_id] from [info] inner join qyml on info.gsid=qyml.id where qyml.user='" + Session["user"].ToString() + "' and type='" + InfoType.Text + "'";

            if (!IsVIP && classdb.GetCount(checkInfoCount) >= 10)
            {
                string responseInfo = string.Format( "<script>alert('您已经添加了10条 [{0}] 信息！如果您想上传更多商机信息，请先升级为VIP会员！');</script>",InfoType.Text);

                Response.Write(responseInfo);
                Response.Write("<script>history.go(-1);</script>");
                Response.End();
                return;
            }
            else
            {
                string sqlGsid = "select id,hybz from qyml where [user]='" + Session["user"] + "'";
                DataTable dataTable = null;

                dataTable = classdb.GetTables(sqlGsid);
                if (dataTable != null && dataTable.Rows.Count > 0)
                {
                    foreach (DataRow row in dataTable.Rows)
                    {
                        Gsid = Convert.ToInt32(row["id"].ToString());
                        Hybz = Convert.ToInt32(row["hybz"].ToString());
                    }
                }

                //flag先设置为1
                string insertSql = "insert into info (showname,period,type,sortid,typeid,[content],linkman,phone,company,address,sf,city,mail,fax,web,jgsm,dateandtime,mobile,gsid,hybz)values('" + showname.Text.Trim() + "','" + period.SelectedValue + "','" + InfoType.SelectedValue + "','" + BigClass.SelectedValue + "','" + SmallClass.SelectedValue + "','" + content.Text + "','" + linkman.Text + "','" + phone.Text + "','" + company.Text + "','" + address.Text + "','" + country.Text + "','" + city.Text + "','" + email.Text + "','" + fax.Text + "','" + web.Text + "','" + jgsm.Text + "','" + DateTime.Now.ToShortDateString() + "','" + mobile.Text + "','" + Gsid + "','" + Hybz + "')";

                if (classdb.GetNonResult(insertSql) != 0)
                {
                    showname.Text = "";
                    Response.Write("<script>alert('操作成功');</script>");
                    Response.Write("<script>history.go(-2);</script>");
                    Response.End();
                }
                else
                {
                    Response.Write("<script>alert('操作失败！请重新填写。');</script>");
                    Response.Write("<script>history.go(-1);</script>");
                    Response.End();
                }
            }

        }


        #region Web 窗体设计器生成的代码
        override protected void OnInit(EventArgs e)
        {
            //
            // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
            //
            InitializeComponent();
            base.OnInit(e);
        }

        /// <summary>
        /// 设计器支持所需的方法 - 不要使用代码编辑器修改
        /// 此方法的内容。
        /// </summary>
        private void InitializeComponent()
        {
            this.Button1.Click += new System.EventHandler(this.Button1_Click);
            this.BigClass.SelectedIndexChanged += new System.EventHandler(this.BigClass_SelectedIndexChanged);
            this.Load += new System.EventHandler(this.Page_Load);

        }
        #endregion
    }
}